package com.servlet;

import com.beans.CateInfo;
import com.beans.GoodsInfo;
import com.dao.CateDao;
import com.dao.GoodsDao;
import net.sf.json.JSONArray;

import javax.json.JsonArray;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "CateServlet.do", value = "/CateServlet.do")
public class CateServlet extends HttpServlet {
    CateDao cateDao = new CateDao();
    GoodsDao goodsDao = new GoodsDao();
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        String flag = request.getParameter("flag");
        if ("getCate".equals(flag)){
            getCate(request,response);
        }else if ("bigCateAdd".equals(flag)){
            bigCateAdd(request,response);
        }else if ("smallCateAdd".equals(flag)){
            smallCateAdd(request,response);
        }else if ("manage".equals(flag)){
            manage(request,response);
        }else if ("deleteCate".equals(flag)){
            deleteCate(request,response);
        }else if ("updateBigCate".equals(flag)){
            updateBigCate(request,response);
        }else if ("updateSmallCate".equals(flag)){
            updateSmallCate(request,response);
        }
    }

    //修改二级分类
    private void updateSmallCate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        int parentId = Integer.parseInt(request.getParameter("parentId"));
        String cateName = request.getParameter("cateName");
        String des = request.getParameter("des");


        int result = cateDao.updateSmallCate(id,cateName,des,parentId);
        if (result==1){
            request.setAttribute("msg","<script>alert('分类修改成功')</script>");
            request.getRequestDispatcher("/goods/smallcate_edit.jsp").forward(request,response);
        }
    }

    //修改一级分类
    private void updateBigCate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        String cateName = request.getParameter("cateName");
        String des = request.getParameter("des");

        int result = cateDao.updateBigCate(id,cateName,des);
        if (result==1){
            request.setAttribute("msg","<script>alert('分类修改成功')</script>");
            request.getRequestDispatcher("/goods/bigcate_edit.jsp").forward(request,response);
        }
    }

    //删除分类
    private void deleteCate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int id = Integer.parseInt(request.getParameter("id"));

        CateInfo cateInfo = cateDao.getCateInfoById(id);
        GoodsInfo goodsInfo = goodsDao.getGoodsByCateId(id,cateInfo);
        if (goodsInfo != null){
            request.setAttribute("msg","<script>alert('该目录下有商品，不能删除')</script>");
            this.manage(request,response);
        }else {
            int result = cateDao.deleteCateById(id);
            cateDao.deleteCateUnderId(id);
            if (result ==1){
                request.setAttribute("msg","<script>alert('删除成功')</script>");
                this.manage(request,response);
            }
        }
    }


    //获取分类列表，并跳转到页面
    private void manage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<CateInfo> cateInfoList = cateDao.getAllCate();
        request.setAttribute("cateInfoList",cateInfoList);
        request.getRequestDispatcher("/goods/goodscate.jsp").forward(request,response);
    }

    //添加二级分类
    private void smallCateAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int parentId = Integer.parseInt(request.getParameter("parentId"));
        String cateName = request.getParameter("cateName");
        String des = request.getParameter("des");

        int result = cateDao.addCate(parentId,cateName,des);
        if (result==1){
            request.setAttribute("msg","<script>alert('分类添加成功')</script>");
            request.getRequestDispatcher("/goods/smallcate_add.jsp").forward(request,response);
        }
    }

    //添加一级分类
    private void bigCateAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int parentId = Integer.parseInt(request.getParameter("parentId"));
        String cateName = request.getParameter("cateName");
        String des = request.getParameter("des");

        int result = cateDao.addCate(parentId,cateName,des);
        if (result==1){
            request.setAttribute("msg","<script>alert('分类添加成功')</script>");
            request.getRequestDispatcher("/goods/bigcate_add.jsp").forward(request,response);
        }
    }

    //查找一级菜单
    private void getCate(HttpServletRequest request, HttpServletResponse response) throws IOException {
        int parentId = Integer.parseInt(request.getParameter("parentId"));

        List<CateInfo> cateList = cateDao.getCateByParentId(parentId);

        JSONArray jsonStr = JSONArray.fromObject(cateList);
        System.out.println(jsonStr);
        response.getWriter().print(jsonStr);
    }
}
